package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.domain.IssueComment;
import com.domain.IssueCommentResponse;
import com.service.IIssueCommentResponseService;
import com.service.IIssueCommentService;
import com.util.UserUtil;
import com.verify.UserVerify;
import com.verify.WeiXinVerify;
import com.websocketOfNotice.msgAnno.Notice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import living.com.result.ApiResult;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 大忽悠
 * @create 2022/2/11 22:43
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "子评论请求")
public class SonCommentController {
   private final IIssueCommentResponseService iIssueCommentResponseService;
  private final IIssueCommentService iIssueCommentService;

  @Notice(id_index = 1,notice_type = "COMMENT_ON_COMMENT")
  @UserVerify
   @WeiXinVerify(paramIndex = 2,paramName = "String",weiXinVerifyType = 1)
   @ApiOperation("在指定父评论下进行评论")
   @PostMapping("/sonComment")
   @Transactional(value = "primaryTransactionManager")
   public ApiResult<Integer> postSonComment(@ApiParam("问题id")@RequestParam Integer issue_id,
           //这里的父评论,就是回答的id
           @ApiParam("父评论id,回答的id")@RequestParam Integer father_comment_id,
                                            @ApiParam("内容")@RequestParam String content)
   {
       IssueComment byId = iIssueCommentService.getById(father_comment_id);
       //父评论要存在
       Assert.isTrue(byId!=null&&byId.getIsDeleted()==0&&byId.getIssueId().equals(issue_id),"回答不存在");
       //字数限制判断
       Assert.isTrue(content.length()>0&&content.length()<=1000,"评论字数在1到1000字之间");
       IssueCommentResponse issueCommentResponse = IssueCommentResponse.builder().fatherCommentId(father_comment_id)
              .content(content).fromUserId(UserUtil.getUserId()).toUserId(byId.getUserId()).build();
       //判断是否是匿名答主进行评论
       if(byId.getIsAnony()==1&&byId.getUserId().equals(UserUtil.getUserId()))
       {
           //如果是匿名答主自己进行评论,那么from和to都对应当前版主自己
           //设置FromUserId为0
           issueCommentResponse.setFromUserId(0);
           issueCommentResponse.setToUserId(0);
       }
      iIssueCommentResponseService.save(issueCommentResponse);
      //对应父评论回复数加一
       byId.setResponseSum(byId.getResponseSum()+1);
       iIssueCommentService.updateById(byId);
      return ApiResult.success(issueCommentResponse.getId());
   }

   //@ApiOperation("删除指定子评论")
   //@DeleteMapping("sonComment")
   public ApiResult<Boolean> delTargetSonComment(@ApiParam("子评论id")@RequestParam Integer son_comment_id)
   {
       boolean removeById = iIssueCommentResponseService.removeById(son_comment_id);
       return ApiResult.success(removeById);
   }

   @UserVerify
    @ApiOperation("逻辑删除指定子评论")
    @DeleteMapping("logicDel")
   public ApiResult<Boolean> logicDelTargetSonComment(@ApiParam("子评论id")@RequestParam Integer son_comment_id)
   {
       Boolean delById = iIssueCommentResponseService.logicDelById(son_comment_id);
       return ApiResult.success(delById);
   }

   @ApiOperation("获取指定父评论下的所有子评论")
   @GetMapping("getFatherSons")
   public ApiResult<Map<String,Object>> getSonCommentUnderTargetFatherComment(
           @ApiParam("父评论id")@RequestParam Integer father_comment_id,
           @ApiParam("儿子起始页")@RequestParam Integer son_startPage,
           @ApiParam("儿子一页多少条数据")@RequestParam Integer son_pageSize,
           @ApiParam("孙子起始页")@RequestParam Integer grant_son_startPage,
           @ApiParam("孙子一页多少条数据")@RequestParam Integer grant_son_pageSize)

   {
        return ApiResult.success(iIssueCommentResponseService
                .getSonCommentUnderTargetFatherComment(father_comment_id,son_startPage,son_pageSize,grant_son_startPage,grant_son_pageSize));
   }
}
